package com.lingyun.financialmanagement.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lingyun.financialmanagement.dao.mapper.BillMapper;
import com.lingyun.financialmanagement.dao.mapper.ComponentPartMapper;
import com.lingyun.financialmanagement.dao.mapper.ProjectMapper;
import com.lingyun.financialmanagement.dao.mapper.UsersMapper;
import com.lingyun.financialmanagement.dao.pojo.Bill;
import com.lingyun.financialmanagement.dao.pojo.ComponentPart;
import com.lingyun.financialmanagement.dao.pojo.Project;
import com.lingyun.financialmanagement.dao.pojo.Users;
import com.lingyun.financialmanagement.vo.ComponentPartExcel;
import com.lingyun.financialmanagement.vo.GeneralLedger;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.util.SerializationUtils;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class CompanyUtils {
    public static ArrayList<GeneralLedger> getCompanyCost(ProjectMapper projectMapper){
        QueryWrapper<Project> projectQueryWrapper = new QueryWrapper<>();
        projectQueryWrapper.orderByAsc("create_time");
        List<Project> projects = projectMapper.selectList(projectQueryWrapper);
        System.out.println(projects);

        // 账号余额默认为0
        BigDecimal accountBalance = new BigDecimal("0");
        // 现金余额
        BigDecimal cashBalance = new BigDecimal("0");
        // 客户总欠款
        BigDecimal clientReceivables = new BigDecimal("0");
        // 公司总欠款
        BigDecimal companyCredit = new BigDecimal("0");

        // 公司应总付税额
        BigDecimal extraTaxAmount = new BigDecimal("0");

        // 库存总税额
        BigDecimal inventoryTaxAmount = new BigDecimal("0");
        ArrayList<GeneralLedger> generalLedgers = new ArrayList<>();

        for (Project project : projects) {
            Timestamp createTime = project.getCreateTime();
            String projectName = project.getName();
            String client = project.getClient();
            // 销售合同
            BigDecimal investmentAmount = project.getInvestmentAmount();
            // 销售合同税额
            BigDecimal investmentAmountTax = project.getInvestmentAmountTax();
            // 采购合同税额
            BigDecimal purchaseContractTax = project.getPurchaseContractTax();
            // 采购合同
            BigDecimal purchaseContract = project.getPublicCost().add(project.getCashCost()).add(project.getCredit());
            // 额外付税
            BigDecimal extraTax = new BigDecimal("0");
            if (investmentAmountTax.subtract(purchaseContractTax).doubleValue()  < 0){
                extraTax = purchaseContractTax.subtract(investmentAmountTax);
            }
            // 公司应总付税额
            extraTaxAmount = extraTaxAmount.add(extraTax);
            // 客户欠款
            BigDecimal receivables = investmentAmount.subtract(project.getPublicCollect()).subtract(project.getCashCollect());
            // 应付账款
            BigDecimal accountsPayable = project.getCredit();
            // 实付公户
            BigDecimal paymentPublic = project.getPublicCost();
            // 实付现金
            BigDecimal paymentCash = project.getCashCost();
            // 公司赊销
            BigDecimal credit = project.getCredit();
            // 公户收款
            BigDecimal publicCollect = project.getPublicCollect();
            // 现金收款
            BigDecimal cashCollect = project.getCashCollect();
            // 项目毛利润
            BigDecimal profit = investmentAmount.subtract(purchaseContract);
            // 项目状态
            String state = project.getState();
            // 账户余额
            accountBalance = accountBalance.add(publicCollect.subtract(paymentPublic));
            // 库存税额
            if (project.getType().equals("warehouse")){
                inventoryTaxAmount = inventoryTaxAmount.add(project.getPurchaseContractTax());
            }else {
                inventoryTaxAmount = inventoryTaxAmount.subtract(extraTax);
            }
            // 现金余额
            cashBalance = cashBalance.add(cashCollect.subtract(paymentCash));
            // 客户总欠款
            clientReceivables = clientReceivables.add(receivables);
            // 公司总欠款
            companyCredit = companyCredit.add(credit);
            // 资金余额
            BigDecimal balance = accountBalance.add(cashBalance);
            // 资产
            BigDecimal property = balance.add(clientReceivables).subtract(companyCredit);
            GeneralLedger generalLedger = new GeneralLedger(createTime, projectName, client, investmentAmount, investmentAmountTax, purchaseContract, purchaseContractTax,extraTax, receivables, accountsPayable, paymentPublic, paymentCash, credit, publicCollect, cashCollect, profit, state,
                    accountBalance, inventoryTaxAmount, cashBalance, clientReceivables, companyCredit, balance, property);

            // 添加到数组
            generalLedgers.add(generalLedger);
        }
        return generalLedgers;
    }

    // 单独项目的excel数据
    public static List<ComponentPartExcel> getComponentPartExcel(List<Project> projects, Integer item, ComponentPartMapper componentPartMapper, BillMapper billMapper, UsersMapper usersMapper) {
        QueryWrapper<ComponentPart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project", projects.get(item).getId());
        queryWrapper.orderByAsc("create_time");
        List<ComponentPart> componentParts = componentPartMapper.selectList(queryWrapper);
        // 创建excel列表模板
        ArrayList<ComponentPartExcel> componentPartExcels = new ArrayList<>();
        for (ComponentPart componentPart : componentParts) {
            ComponentPartExcel componentPartExcel = new ComponentPartExcel();

            componentPartExcel.setSerialNumber(componentPart.getSerialNumber());
            componentPartExcel.setName(componentPart.getName());
            componentPartExcel.setNumber(componentPart.getNumber());
            componentPartExcel.setUnitPrice(componentPart.getUnitPrice());
            componentPartExcel.setCostFront(componentPart.getCostFront());
            // 设置发票
            Integer billId = componentPart.getBill();
            Bill bill = billMapper.selectById(billId);
            componentPartExcel.setBill(bill.getName() + "  --  " + bill.getTaxRate());
            // 设置用户名
            Users users = usersMapper.selectById(componentPart.getUsers());
            componentPartExcel.setUsers(users.getName());

            componentPartExcel.setPaymentMethod(componentPart.getPaymentMethod());
            componentPartExcel.setCompany(componentPart.getCompany());
            componentPartExcel.setType(componentPart.getType());
            componentPartExcel.setRemark(componentPart.getRemark());
            componentPartExcel.setCreateTime(componentPart.getCreateTime());
            componentPartExcels.add(componentPartExcel);
        }

        return componentPartExcels;
    }
}
